#include "Colors.h"
#include "utils\Log.h"
#include <stdio.h>
#include "base\Framework.h"
#include "base\Engine.h"
#include "game\World.h"
#include "utils\Timer.h"
#include "SimpleHighscoreService.h"
#include "RunState.h"
#include "HUD.h"
#include "addons\ScreenQuad.h"
#include "addons\BasicGameState.h"
#include "Background.h"
#include "pipeline\BasicGameStateParser.h"
#include "pipeline\MenuComponentParser.h"
#include "addons\MenuComponent.h"
#include "GameActions.h"

Colors::Colors() : ds::CGame() {
}

void Colors::loadContent(ds::ResManager& resourceManager) {	
	//gEngine->getAudioManager().play("Opium",100,true);
	gEngine->getActionManager().startAction("UpdateBackground");
}

void Colors::initialize() {
	LOG(logINFO) << "Starting game";	
	gEngine->addService(new ds::CTimer());

	m_Core = new GameCore();
	gEngine->getActionManager().addAction(new BackgroundAction());
	gEngine->getActionManager().addAction(new FadeAction());
	gEngine->getActionManager().addAction(new GameOverMenuAction("TextureArrayMaterial","EndMenu",m_Core));
	gEngine->getActionManager().addAction(new HighscoreMenuAction("TextureArrayMaterial","StartMenu",m_Core));

	gEngine->getResourceManager().loadFont("XScale","content\\fonts\\X-SCALE_.TTF","xscale",30,false);
	gEngine->getResourceManager().loadFont("BigXScale","content\\fonts\\X-SCALE_.TTF","xscale",48,false);	
	
	ds::ScreenQuad* m_Background = new ds::ScreenQuad("BackBuffer","BackgroundMaterial",10);
	m_Background->add(ds::Vec3(0,0,1),ds::Vec3(1024,768,0),ds::Rect(0,0,1024,1024));		
	gEngine->getWorld().attachNode(m_Background,"RTLayer");

	gEngine->addGameState(new RunState("Run",m_Core));	
	gEngine->activateGameState("Run");		
}

//-----------------------------------------------------------------------------
// Name: wWinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE, LPWSTR, INT ) {	
	
#ifdef DEBUG
	FILE* pFile = fopen("application.log", "w");
	FileOutputHandler::Stream() = pFile;
	LOG(logINFO) << "Starting game";	
	pFile = fopen("application.log", "a");
	FileOutputHandler::Stream() = pFile;
#endif

	ds::Settings settings;
	settings.width = 1024;
	settings.height = 768;
	settings.title = "Colors";
	settings.mode = 1;
	settings.synched = true;
	//settings.fullscreen = true;
	//settings.clearColor = ds::Color(0.87f,0.87f,0.87f,1.0f);
	settings.clearColor = ds::Color(0.0f,0.0f,0.0f,1.0f);
	Colors *st = new Colors();
	ds::CFramework *framework = new ds::CFramework(st);
	framework->initialize(hInstance,settings);
	LOG(logINFO) << "Running game now";
	framework->Run();
	LOG(logINFO) << "Game ended";
	framework->Release();
	delete framework;
	return 0; 
}
